package cn.edu.pzhu.dao.imp;

import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.edu.pzhu.dao.FileMsgDAO;
import cn.edu.pzhu.pojo.FileMsg;
import cn.edu.pzhu.util.DruidUtil;

public class FileMsgDAOImp implements FileMsgDAO{
	
	private JdbcTemplate template = new JdbcTemplate(DruidUtil.getDataSource());
	
	@Override
	public int insert(FileMsg t) {
		String sql = "insert into filemsg (username, filename, classification, filepath, releasedate, rating, description) values(?,?,?,?,?,?,?)";
		try {
			return template.update(sql, t.getUsername(),t.getFilename(),t.getClassification(),t.getFilepath(),t.getReleasedate(),t.getRating(),t.getDescription());
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public int update(Integer key, FileMsg t) {
		String sql = "update t set username=?, filename=?, classification=?, filepath=?, releasedate=?, rating=?, description=? where fileid=?";
		try {
			return template.update(sql,t.getUsername(),t.getFilename(),t.getClassification(),t.getFilepath(),t.getReleasedate(),t.getRating(),t.getDescription(),key);
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public FileMsg selectById(Integer key) {
		String sql="select * from filemsg where fileid=?";
		try {
			return template.queryForObject(sql, new BeanPropertyRowMapper<>(FileMsg.class),key);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<FileMsg> selectAll() {
		String sql = "select * from filemsg";
		try {
			return template.query(sql, new BeanPropertyRowMapper<>(FileMsg.class));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<FileMsg> selectByKeyword(String keyword) {
		List<FileMsg> list = null;
		String sql = "select * from filemsg where concat(username,filename,classification,IFNULL(description,'')) like ?";
		try {
			list = template.query(sql, new BeanPropertyRowMapper<>(FileMsg.class),keyword);
		} catch (Exception e) {
			//没有查到任何用户，会抛出异常
		}		
		return list;
	}

	@Override
	public List<FileMsg> selectByCategories(String categories) {
		List<FileMsg> list = null;
		String sql = "select * from filemsg where classification=?";
		try {
			list = template.query(sql, new BeanPropertyRowMapper<>(FileMsg.class),categories);
		} catch (Exception e) {
			//没有查到任何用户，会抛出异常
		}		
		return list;
	}

	@Override
	public List<FileMsg> selectByUsername(String username) {
		List<FileMsg> list = null;
		String sql = "select * from filemsg where username=?";
		try {
			list = template.query(sql, new BeanPropertyRowMapper<>(FileMsg.class),username);
		} catch (Exception e) {
			//没有查到任何用户，会抛出异常
		}		
		return list;
	}

	@Override
	public int destroy(String filename) {
		String sql = "delete from filemsg where filename = ?";
		try {
			return template.update(sql,filename);
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public String selectFilePath(String filename, String username) {
		String sql = "select filepath from filemsg where filename=? and username=?";
		try {
			return template.queryForObject(sql, String.class,filename,username);
		} catch (Exception e) {
			return null;
		}		
	}

}
